package org.jeecg.modules.demo.dary.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.util.TreeBuilder;
import com.google.gson.JsonArray;
import org.jeecg.modules.demo.api.mapper.ApiInfoMapper;
import org.jeecg.modules.demo.dajy.mapper.DaJyxxMapper;
import org.jeecg.modules.demo.dary.entity.DaRyxx;
import org.jeecg.modules.demo.dary.mapper.DaRyxxMapper;
import org.jeecg.modules.demo.dary.service.IDaRyxxService;
import org.jeecg.modules.demo.dwxx.service.ISysDwxxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 人员档案信息表
 * @Author: jeecg-boot
 * @Date:   2023-11-17
 * @Version: V1.0
 */
@Service
public class DaRyxxServiceImpl extends ServiceImpl<DaRyxxMapper, DaRyxx> implements IDaRyxxService {
//    @Autowired
//    private DaJyxxMapper DjyMapper;
    @Autowired
    private DaRyxxMapper DaRyxxMapper;

    public JSONObject jyupzt(String id ,String zt){
        JSONObject tmpRet = new JSONObject();
        try{
            DaRyxxMapper.jyupxx(id,zt);
            tmpRet.put("msg", "ok");
            tmpRet.put("code", "0");
        }
        catch(Exception ex){
            tmpRet.put("code", "1");
            tmpRet.put("msg", ex.toString());
        }
        return tmpRet;
    }

    public JSONObject daml(String user ,String orgcode,String mbmc, String ryid){
        JSONObject tmpRet = new JSONObject();
        try{
            tmpRet.put("data", DaRyxxMapper.daml(user,orgcode,mbmc,ryid));
            tmpRet.put("code", "0");
        }
        catch(Exception ex){
            tmpRet.put("code", "1");
            tmpRet.put("msg", ex.toString());
        }
        return tmpRet;
    }
    public List<TreeBuilder.Node> sqrydaml(String orgcode,String ryxm){
        JSONObject tmpRet = new JSONObject();
        List<TreeBuilder.Node> nodeList = new ArrayList<>();
        try{
            JsonArray answer = new JsonArray();
            System.out.println("ryxm="+ryxm);
            System.out.println("orgcode="+orgcode);
            List<Map<String, Object>> temp = DaRyxxMapper.getrylist(orgcode,ryxm,"","","","","","","","","","","",
                    "","","","","","","","","","");
            System.out.println("temp="+temp);
            for (Map<String, Object> map : temp){
                String mid = map.get("id").toString();
                String parent_id =  "-1";
                String depart_name = map.get("xm").toString();
                String code = map.get("id").toString();
                String depart_name_en = map.get("xm").toString();
                String org_type =  "1";
                String ml_sfzh = "";
                if (map.get("sfzh")!=null){ml_sfzh = map.get("sfzh").toString();};
                String ml_rwsj = "";
                if (map.get("rwsj")!=null){ml_rwsj = map.get("rwsj").toString();};
                String ml_twsj = "";
                if (map.get("twsj")!=null){ml_twsj = map.get("twsj").toString();};
                String ml_lxdh = "";
                if (map.get("lxdh")!=null){ml_lxdh = map.get("lxdh").toString();};

//                TreeBuilder.Node node = new TreeBuilder.Node(mid, parent_id, depart_name,code, org_type, mid, depart_name_en, false,parent_id,"-1","-1");
//                nodeList.add(node);
                List<Map<String, Object>> memp = DaRyxxMapper.getrymllist(mid);
                for (Map<String, Object> map1 : memp){
                    String ml_mid = map1.get("ryid").toString()+map1.get("mlid").toString();
                    String ml_parent_id =  map1.get("pid").toString();
                    if (ml_parent_id.equals("0")){
                        ml_parent_id = mid;
                    }else{
                        ml_parent_id = map1.get("ryid").toString()+map1.get("pid").toString();
                    }
                    String ml_depart_name = map1.get("mlmc").toString();
                    String ml_code = map1.get("id").toString();
                    String ml_orgcode = "";
                    if (map1.get("org_code")!=null){ml_orgcode = map1.get("org_code").toString();};



                    String ml_depart_name_en = depart_name_en;
                    String ml_org_type =  "2";

                    TreeBuilder.Node ml_node = new TreeBuilder.Node(ml_mid, ml_parent_id, ml_depart_name,ml_code, ml_org_type, mid, ml_depart_name_en, false,
                            map1.get("pid").toString(),map1.get("mlid").toString(),map1.get("id").toString(),ml_orgcode,ml_sfzh,ml_rwsj,ml_twsj,ml_lxdh);
                    nodeList.add(ml_node);
                }

            }

            tmpRet.put("code", "0");
        }
        catch(Exception ex){
            tmpRet.put("code", "1");
            tmpRet.put("msg", ex.toString());
        }
        return nodeList;
    }
    public List<TreeBuilder.Node> rydaml(String orgcode,String ryxm,String id){
        JSONObject tmpRet = new JSONObject();
        List<TreeBuilder.Node> nodeList = new ArrayList<>();
        try{
            JsonArray answer = new JsonArray();

            List<Map<String, Object>> temp = DaRyxxMapper.getrylist(orgcode,ryxm,id,"","","","","","","","","","","","","",
                    "","","","","","","");

            for (Map<String, Object> map : temp){
                String mid = map.get("id").toString();
                String parent_id =  "-1";
                String depart_name = map.get("xm").toString();
                String code = map.get("id").toString();
                String depart_name_en = map.get("xm").toString();
                String org_type =  "1";
                String ml_sfzh = "";
                if (map.get("sfzh")!=null){ml_sfzh = map.get("sfzh").toString();};
                String ml_rwsj = "";
                if (map.get("rwsj")!=null){ml_rwsj = map.get("rwsj").toString();};
                String ml_twsj = "";
                if (map.get("twsj")!=null){ml_twsj = map.get("twsj").toString();};
                String ml_lxdh = "";
                if (map.get("lxdh")!=null){ml_lxdh = map.get("lxdh").toString();};
                TreeBuilder.Node node = new TreeBuilder.Node(mid, parent_id, depart_name,code, org_type, mid, depart_name_en, false,parent_id,"-1","-1","","","","","");
                nodeList.add(node);
                List<Map<String, Object>> memp = DaRyxxMapper.getrymllist(mid);
                for (Map<String, Object> map1 : memp){
                    String ml_mid = map1.get("ryid").toString()+map1.get("mlid").toString();
                    String ml_parent_id =  map1.get("pid").toString();
                    if (ml_parent_id.equals("0")){
                        ml_parent_id = mid;
                    }else{
                        ml_parent_id = map1.get("ryid").toString()+map1.get("pid").toString();
                    }
                    String ml_depart_name = map1.get("mlmc").toString();
                    String ml_code = map1.get("id").toString();
                    String ml_orgcode = "";
                    if (map1.get("org_code")!=null){ml_orgcode = map1.get("org_code").toString();};
                    String ml_depart_name_en = depart_name_en;
                    String ml_org_type =  "2";

                    TreeBuilder.Node ml_node = new TreeBuilder.Node(ml_mid, ml_parent_id, ml_depart_name,ml_code, ml_org_type, mid, ml_depart_name_en, false,map1.get("pid").toString(),map1.get("mlid").toString(),
                            map1.get("id").toString(),ml_orgcode,ml_sfzh,ml_rwsj,ml_twsj,ml_lxdh);
                    nodeList.add(ml_node);
                }

            }

            tmpRet.put("code", "0");
        }
        catch(Exception ex){
            tmpRet.put("code", "1");
            tmpRet.put("msg", ex.toString());
        }
        return nodeList;
    }

    public List<TreeBuilder.Node> ckrydaml(String orgcode,String ryxm,String id,String zt ,String sfzh,String xb,String csrqks,String csrqjs,
                                           String mz,String rwsjks,String rwsjjs,String twsjks,String twsjjs,String zzmm,String lxdh,String  xlqk,String  jkzt,
                                           String  cjxz,String  bddh,String twzh,String  azfs,String jl,String  rwgxmc){
        JSONObject tmpRet = new JSONObject();
        List<TreeBuilder.Node> nodeList = new ArrayList<>();
        try{
            JsonArray answer = new JsonArray();

            List<Map<String, Object>> temp = DaRyxxMapper.getrylist(orgcode,ryxm,id,zt,sfzh, xb, csrqks, csrqjs,
                     mz, rwsjks, rwsjjs, twsjks, twsjjs, zzmm, lxdh,  xlqk,  jkzt,
                      cjxz,  bddh, twzh,  azfs, jl,  rwgxmc);
            for (Map<String, Object> map : temp){
                String mid = map.get("id").toString();
                String parent_id =  "-1";
                String depart_name = map.get("xm").toString();
                String code = map.get("id").toString();
                String depart_name_en = map.get("xm").toString();
                String org_type =  "1";
                String ml_sfzh = "";
                if (map.get("sfzh")!=null){ml_sfzh = map.get("sfzh").toString();};
                String ml_rwsj = "";
                if (map.get("rwsj")!=null){ml_rwsj = map.get("rwsj").toString();};
                String ml_twsj = "";
                if (map.get("twsj")!=null){ml_twsj = map.get("twsj").toString();};
                String ml_lxdh = "";
                if (map.get("lxdh")!=null){ml_lxdh = map.get("lxdh").toString();};
                TreeBuilder.Node node = new TreeBuilder.Node(mid, parent_id, depart_name,code, org_type, mid, depart_name_en, false,parent_id,"-1","-1","","","","","");
                nodeList.add(node);
//                List<String> mlidlist = new ArrayList<String>();
                List<Map<String, Object>> memp = DaRyxxMapper.getckryml(mid);
                for (Map<String, Object> map1 : memp){
                    String ml_id = map1.get("id").toString();

                    String ml_mid = map1.get("ryid").toString()+map1.get("mlid").toString();
//                    mlidlist.add(ml_id);
                    String ml_parent_id =  map1.get("pid").toString();
                    if (ml_parent_id.equals("0")){
                        ml_parent_id = mid;
                    }else{
                        ml_parent_id = map1.get("ryid").toString()+map1.get("pid").toString();
                    }
                    String ml_depart_name = map1.get("mlmc").toString();
                    String ml_code = map1.get("id").toString();
                    String ml_orgcode = "";
                    if (map1.get("org_code")!=null){ml_orgcode = map1.get("org_code").toString();};
                    String ml_depart_name_en = depart_name_en;
                    String ml_org_type =  "2";
//                    if (!mlidlist.contains(ml_id)) {
                        TreeBuilder.Node ml_node = new TreeBuilder.Node(ml_mid, ml_parent_id, ml_depart_name, ml_code, ml_org_type, mid, ml_depart_name_en, false, map1.get("pid").toString(), map1.get("mlid").toString(),
                                map1.get("id").toString(), ml_orgcode, ml_sfzh, ml_rwsj, ml_twsj, ml_lxdh);
                        nodeList.add(ml_node);
//                    }
                }

            }

            tmpRet.put("code", "0");
        }
        catch(Exception ex){
            tmpRet.put("code", "1");
            tmpRet.put("msg", ex.toString());
        }
        return nodeList;
    }

    public JSONObject rydamladd(String user ,String orgcode,String ryid,String mlmc,String pid){
        JSONObject tmpRet = new JSONObject();
        try{
            tmpRet.put("data", DaRyxxMapper.rydamladd(user,orgcode,mlmc,ryid,pid));
            tmpRet.put("code", "0");
        }
        catch(Exception ex){
            tmpRet.put("code", "1");
            tmpRet.put("msg", ex.toString());
        }
        return tmpRet;
    }
    public JSONObject rydamledit(String user ,String orgcode,String id,String mlmc){
        JSONObject tmpRet = new JSONObject();
        try{
            tmpRet.put("data", DaRyxxMapper.rydamledit(user,orgcode,mlmc,id));
            tmpRet.put("code", "0");
        }
        catch(Exception ex){
            tmpRet.put("code", "1");
            tmpRet.put("msg", ex.toString());
        }
        return tmpRet;

    }
    public JSONObject rydamldel(String user ,String orgcode,String id,String ryid,String pid){
        JSONObject tmpRet = new JSONObject();
        try{
            List<Map<String, Object>> memp =  DaRyxxMapper.getryml(ryid,pid);
            if (memp.size()>0){
                tmpRet.put("code", "1");
                tmpRet.put("msg", "当前目录拥有下级目录，不能删除");
            }else{
                tmpRet.put("data", DaRyxxMapper.rydamldel(user,orgcode,id));
                tmpRet.put("code", "0");
            }

        }
        catch(Exception ex){
            tmpRet.put("code", "1");
            tmpRet.put("msg", ex.toString());
        }
        return tmpRet;

    }
    public Page<DaRyxx> getlist(Page<DaRyxx>page , String xm,String zt,String  sfzh,String  xb,String  csrqks,String  csrqjs,String  mz,String  rwsjks,String  rwsjjs,
                                String  twsjks,String  twsjjs,String  zzmm,String  lxdh,String  xlqk,String  jkzt,String  cjxz,String  bddh,
                                String  twzh,String  azfs,String  jl,String  rwgxmc){
        return page.setRecords(this.DaRyxxMapper.getlist(page,xm,zt,sfzh,xb,csrqks,csrqjs,mz,rwsjks,rwsjjs,twsjks,twsjjs,zzmm,lxdh,xlqk,jkzt,cjxz,bddh,twzh,azfs,jl,rwgxmc));
    }
}
